/**
 * @Title: UIHelper.java
 * @Package: com.jason.fingerprint.common
 * @Descripton: TODO
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月4日09:49:14
 * @Version: V1.0
 */
package net.hzlj.platform.enterprise.helper;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.text.format.DateFormat;

import net.hzlj.platform.enterprise.BootApp;
import net.hzlj.platform.enterprise.beans.ui.FileBean;
import net.hzlj.platform.enterprise.common.Constant;
import net.hzlj.platform.enterprise.utils.AppPreferencesUtils;
import net.hzlj.platform.enterprise.utils.FileUtils;
import net.hzlj.platform.enterprise.utils.PreferencesUtils;

import java.io.File;
import java.util.Calendar;
import java.util.Locale;

/**
 * @ClassName: UIHelper
 * @Description: 应用文件工具
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2014年11月4日09:49:20
 */
public class FileHelper {

	private static final String TAG = FileHelper.class.getSimpleName();

	public static final String ROOT_FILE_NAME = Constant.PROJECT_NAME;// 文件存储根路径
	public static final String IMAGES_FILE_NAME = "Images";// 拍照存储路径
	public static final String AUDIOS_FILE_NAME = "Audios";// 录音存储路径
	public static final String CACHES_FILE_NAME = "Caches";// 网络图片缓存路径
	public static final String APKS_FILE_NAME = "Apks";// 应用更新存储路径
	public static final String SQL_FILE_NAME = "Sql";// 数据库保存位置
	public static final String LOG_FILE_NAME = "Log";// 日志保存位置


	// 获取应用缓存根目录下
	public static File getRootDir() {
		String account = AppPreferencesUtils.getString(BootApp.getAppContext(),PreferencesUtils.KEY_USER_ACCOUNT,"0");
		return FileUtils.getDiskCacheDir(BootApp.getAppContext(),new String[]{ROOT_FILE_NAME,account});
	}

	// 获取应用缓存根目录下R
	public static File getImagesDir() {
		String account = AppPreferencesUtils.getString(BootApp.getAppContext(),PreferencesUtils.KEY_USER_ACCOUNT,"0");
		return FileUtils.getDiskCacheDir(BootApp.getAppContext(),new String[]{ROOT_FILE_NAME,account,IMAGES_FILE_NAME});
	}

	// 获取应用缓存根目录下
	public static File getAudiosDir() {
		String account = AppPreferencesUtils.getString(BootApp.getAppContext(),PreferencesUtils.KEY_USER_ACCOUNT,"0");
		return FileUtils.getDiskCacheDir(BootApp.getAppContext(),new String[]{ROOT_FILE_NAME,account,AUDIOS_FILE_NAME});
	}

	// 获取应用缓存根目录下
	public static File getCachesDir() {
		String account = AppPreferencesUtils.getString(BootApp.getAppContext(),PreferencesUtils.KEY_USER_ACCOUNT,"0");
		return FileUtils.getDiskCacheDir(BootApp.getAppContext(),new String[]{ROOT_FILE_NAME,account,CACHES_FILE_NAME});
	}

	// 获取更新的apk
	public static File getApksDir() {
		return FileUtils.getDiskCacheDir(BootApp.getAppContext(),new String[]{ROOT_FILE_NAME,APKS_FILE_NAME});
	}

	//数据库
	public static File getSqlDir(){
		String account = AppPreferencesUtils.getString(BootApp.getAppContext(),PreferencesUtils.KEY_USER_ACCOUNT,"0");
		return FileUtils.getDiskCacheDir(BootApp.getAppContext(),new String[]{ROOT_FILE_NAME,account,SQL_FILE_NAME});
	}

	//日志
	public static File getLogDir(){
		return FileUtils.getDiskCacheDir(BootApp.getAppContext(),new String[]{ROOT_FILE_NAME,LOG_FILE_NAME});
	}

	//获取App根目录
	public static String getAppRootDir(Context context) {
		return FileUtils.getDiskCacheDir(context, ROOT_FILE_NAME)
				.getAbsolutePath();
	}

	//获取SD下项目根路径
	public static String getSdRootDir(Context context) {
		return FileUtils.getDiskCacheDir(context, ROOT_FILE_NAME)
				.getAbsolutePath();
	}

	// 获取应用根目录下指定文件夹名字的文件
	public static File getFileFromName(Context context, String dirName) {
		File rootFile = FileUtils.getDiskCacheDir(context, ROOT_FILE_NAME);
		if (!rootFile.exists()) {
			rootFile.mkdirs();
		}
		String account = "0";
		File accountFile = new File(rootFile, account);
		if (!accountFile.exists()) {
			accountFile.mkdirs();
		}
		File childFile = new File(rootFile, dirName);
		if (!childFile.exists()) {
			childFile.mkdirs();
		}
		return childFile;
	}

	// 获取图片随机名字
	public static String getPhotoFileName() {
		String photoName = DateFormat.format("yyyyMMdd_hhmmss",
				Calendar.getInstance(Locale.CHINA))
				+ ".jpg";
		return photoName;
	}

	// 获取一个空的图片文件
	public static File getPhotoFile() {
		File photoFile = new File(FileHelper.getImagesDir(),
				FileHelper.getPhotoFileName());
		return photoFile;
	}

	/***
	 * 获取最新拍照的图片信息
	 * @Descripton: TODO
	 * @Param @return
	 * @Return $
	 * @Throws
	 */
	public static String getTheNewPhotoPath(Context context){
		Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
		ContentResolver mContentResolver = context.getContentResolver();
		String path = null;
		try {
			Cursor mCursor = mContentResolver.query(mImageUri, null,
					null,null,
					MediaStore.Images.Media.DATE_ADDED);
			mCursor.moveToFirst();
			path = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.DATA));
			if (mCursor != null) {
				mCursor.close();
			}
		} catch (Exception e) {

		}
		return path;
	}


	/***
	 * 删除指定文件
	 * @param path
	 */
	public static void deleteFile(String path){
		if (!TextUtils.isEmpty(path)){
			try {
				File file = new File(path);
				if (file != null && file.exists()){
					file.delete();
				}
			} catch (Exception e){
				e.printStackTrace();
			}
		}
	}

	/***
	 * 根据文件路径获取文件信息
	 */
	public static FileBean getFileBeanByPath(
			Context context, String path, int fileType) {
		Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
		FileBean bean = new FileBean();
		ContentResolver mContentResolver = context.getContentResolver();
		// 只查询jpeg和png的图片
		if (!TextUtils.isEmpty(path)){
			Cursor mCursor = mContentResolver.query(mImageUri, null,
					MediaStore.Images.Media.DATA + "=?", new String[] { path },
					MediaStore.Images.Media.DATE_MODIFIED);
			mCursor.moveToFirst();
			String title = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.TITLE));
			String disName = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
			String type = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.MIME_TYPE));
			String id = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media._ID));
			String addDate = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.DATE_ADDED));
			String modifyData = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.DATE_MODIFIED));
			String height = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.HEIGHT));
			String width = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.WIDTH));
			String size = mCursor.getString(mCursor
					.getColumnIndex(MediaStore.Images.Media.SIZE));
			bean.setCreateDate(String.valueOf(System.currentTimeMillis()));
			bean.setFileName(title);
			bean.setFilePath(path);
			bean.setFileType(fileType);
			bean.setState(1);
			if (mCursor != null) {
				mCursor.close();
			}
		}
		return bean;
	}

}
